
<template>
  <div class="app-container">
    <el-row :gutter="20" class="main-row">
      <el-col :span="span" :class="['span1', 'left-section']">
        <div class="booking-section">
          <div class="section-header">
            <h2 class="section-title">✈️ 航班预订管理</h2>
            <div class="booking-count">共 {{ tableData.length }} 条预订</div>
          </div>

          <div class="bookings-container">
            <div
              v-for="booking in tableData"
              :key="booking.bookingNumber"
              class="booking-card-item"
            >
              <div class="booking-card-header">
                <div class="booking-number">
                  <span class="label">订单号</span>
                  <span class="value">#{{ booking.bookingNumber }}</span>
                </div>
                <el-tag
                  :type="booking.bookingStatus === 'CONFIRMED' ? 'success' : 'warning'"
                  size="large"
                  effect="dark"
                >
                  {{ booking.bookingStatus === "CONFIRMED" ? "已确认" : "待确认"}}
                </el-tag>
              </div>

              <div class="booking-card-body">
                <div class="passenger-info">
                  <div class="info-icon">👤</div>
                  <div class="info-content">
                    <span class="info-label">乘客姓名</span>
                    <span class="info-value">{{ booking.name }}</span>
                  </div>
                </div>

                <div class="flight-route">
                  <div class="route-point">
                    <div class="point-icon">🛫</div>
                    <div class="point-info">
                      <span class="point-label">出发</span>
                      <span class="point-value">{{ booking.from }}</span>
                    </div>
                  </div>

                  <div class="route-arrow">
                    <div class="arrow-line"></div>
                    <div class="arrow-head">→</div>
                  </div>

                  <div class="route-point">
                    <div class="point-icon">🛬</div>
                    <div class="point-info">
                      <span class="point-label">到达</span>
                      <span class="point-value">{{ booking.to }}</span>
                    </div>
                  </div>
                </div>

                <div class="booking-details">
                  <div class="detail-item">
                    <span class="detail-icon">📅</span>
                    <span class="detail-text">{{ booking.date }}</span>
                  </div>
                  <div class="detail-item">
                    <span class="detail-icon">💺</span>
                    <span class="detail-text">{{ booking.bookingClass }}</span>
                  </div>
                </div>
              </div>

              <div class="booking-card-footer">
                <el-button size="small" type="primary" round>
                  更改预订
                </el-button>
                <el-button size="small" type="danger" round plain>
                  取消预订
                </el-button>
              </div>
            </div>

            <div v-if="tableData.length === 0" class="empty-state">
              <div class="empty-icon">✈️</div>
              <div class="empty-text">暂无预订记录</div>
            </div>
          </div>
        </div>

        <div style="position: relative; width: 100%; height: 100%">
          <el-drawer
              class="drawerClass"
              style="position: absolute"
              :append-to-body="false"
              :modal="false"
              :show-close="false"
              :wrapperClosable="false"
              size="100%"
              :visible.sync="drawer"
              direction="ltr"
          >
            <el-card class="box-card">
              <div>左侧内容</div>
            </el-card>
          </el-drawer>
          <div
              class="toggle-button"
              :class="[drawer ? 'imgright' : 'imgright1']"
              @click="clickImg"
          >
            <img
                v-show="!drawer"
                style="height: 40px; width: 25px"
                src="./assets/zhankai.png"
                alt="展开"
            />
            <img
                v-show="drawer"
                style="height: 40px; width: 25px"
                src="./assets/shousuo.png"
                alt="收缩"
            />
          </div>
        </div>
      </el-col>

      <el-col :span="span1" :class="['span1', 'chat-section']">
        <div class="chat-container">
          <div class="chat-header">
            <h3 class="chat-title">🤖 智能客服助手</h3>
            <div class="chat-status">
              <span class="status-dot"></span>
              <span class="status-text">在线</span>
            </div>
          </div>

          <div class="messages-container" ref="messagesContainer">
            <div
                v-for="(activity, index) in activities"
                :key="index"
                :class="['message-item', activity.type === 'primary' ? 'user-message' : 'bot-message']"
            >
              <div class="message-bubble">
                <div class="message-content">{{ activity.content }}</div>
                <div class="message-time">{{ activity.timestamp }}</div>
              </div>
            </div>
          </div>

          <div class="input-container">
            <div class="input-wrapper">
              <el-input
                  v-model="msg"
                  class="chat-input"
                  type="textarea"
                  :rows="2"
                  placeholder="请输入您的问题..."
                  @keydown.enter.prevent="sendMsg()"
                  resize="none"
              />
              <el-button
                  class="send-button"
                  type="primary"
                  circle
                  @click="sendMsg()"
                  :disabled="!msg.trim()"
              >
                <span class="send-icon">📤</span>
              </el-button>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script lang="ts">
import { MoreFilled } from '@element-plus/icons-vue'
import {ref, onMounted} from "vue";
import axios from 'axios'//引入axios

export default {
  data() {
    return {
      span: 15,
      span1: 8,
      drawer: true,
    };
  },
  methods: {
    clickImg() {
      this.drawer = !this.drawer;
      if (this.drawer) {
        this.span = 15;
        this.span1 = 8;
      } else {
        this.span = 1;
        this.span1 = 23;
      }
    },
  },
  setup() {
    const activities = ref([
      {
        content: '⭐欢迎来到Artisan航空✈！请问有什么可以帮您的?',
        timestamp: new Date().toLocaleDateString() + " " + new Date().toLocaleTimeString(),
        color: '#0bbd87',
      },
    ]);
    const msg = ref('');
    const tableData = ref([]);
    let count = 2;
    let eventSource;

    const sendMsg = () => {
      if (eventSource) {
        eventSource.close();
      }

      activities.value.push(
          {
            content: `你:${msg.value}`,
            timestamp: new Date().toLocaleDateString() + " " + new Date().toLocaleTimeString(),
            size: 'large',
            type: 'primary',
            icon: MoreFilled,
          },
      );

      activities.value.push(
          {
            content: 'waiting...',
            timestamp: new Date().toLocaleDateString() + " " + new Date().toLocaleTimeString(),
            color: '#0bbd87',
          },
      );

      // sse: 服务端推送 Server-Sent Events
      eventSource = new EventSource(`http://localhost:8080/ai/generateStreamAsString?message=${msg.value}`);
      msg.value='';
      eventSource.onmessage = (event) => {
        activities.value[count].content += event.data;
      };
      eventSource.onopen = (event) => {
        activities.value[count].content = '';
      };
      eventSource.onerror = function (e) {
        count = count + 2;
        eventSource.close();
        getBookings();  // 每次对话完后刷新列表
      };
    };

    const getBookings = () => {
      axios.get('http://localhost:8080/booking/list')
          .then((response) => {
            debugger;
            tableData.value = response.data;
          })
          .catch((error) => {
            console.error(error);
          });
    };

    // Use onMounted to call getBookings when the component is mounted
    onMounted(() => {
      getBookings();
    });

    return {
      activities,
      msg,
      tableData,
      sendMsg,
      getBookings,
    };
  },
};
</script>
<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.app-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #2193b0 0%, #6dd5ed 100%);
  padding: 20px;
}

.main-row {
  height: calc(100vh - 40px);
}

/* 左侧区域 */
.left-section {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.booking-section {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  height: 100%;
  display: flex;
  flex-direction: column;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 2px solid #e5e7eb;
}

.section-title {
  font-size: 24px;
  font-weight: 600;
  color: #2d3748;
  margin: 0;
}

.booking-count {
  background: linear-gradient(135deg, #2193b0 0%, #6dd5ed 100%);
  color: white;
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
}

.bookings-container {
  flex: 1;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 16px;
  padding: 4px;
}

.bookings-container::-webkit-scrollbar {
  width: 6px;
}

.bookings-container::-webkit-scrollbar-track {
  background: transparent;
}

.bookings-container::-webkit-scrollbar-thumb {
  background: rgba(33, 147, 176, 0.3);
  border-radius: 3px;
}

.bookings-container::-webkit-scrollbar-thumb:hover {
  background: rgba(33, 147, 176, 0.5);
}

/* 预订卡片 */
.booking-card-item {
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  overflow: hidden;
  border: 1px solid #e5e7eb;
  display: flex;
  flex-direction: column;
  height: fit-content;
}

.booking-card-item:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(33, 147, 176, 0.2);
  border-color: #2193b0;
}

.booking-card-header {
  background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
  padding: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #e5e7eb;
}

.booking-number {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.booking-number .label {
  font-size: 12px;
  color: #64748b;
}

.booking-number .value {
  font-size: 16px;
  font-weight: 600;
  color: #2193b0;
}

.booking-card-body {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.passenger-info {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  background: #f8fafc;
  border-radius: 8px;
}

.info-icon {
  font-size: 24px;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: white;
  border-radius: 8px;
}

.info-content {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.info-label {
  font-size: 12px;
  color: #64748b;
}

.info-value {
  font-size: 16px;
  font-weight: 600;
  color: #2d3748;
}

/* 航线信息 */
.flight-route {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
  border-radius: 8px;
}

.route-point {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
}

.point-icon {
  font-size: 20px;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: white;
  border-radius: 8px;
}

.point-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.point-label {
  font-size: 11px;
  color: #64748b;
}

.point-value {
  font-size: 14px;
  font-weight: 600;
  color: #2193b0;
}

.route-arrow {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.arrow-line {
  width: 40px;
  height: 2px;
  background: linear-gradient(to right, #2193b0, #6dd5ed);
}

.arrow-head {
  font-size: 18px;
  color: #2193b0;
  font-weight: bold;
}

/* 预订详情 */
.booking-details {
  display: flex;
  gap: 12px;
}

.detail-item {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px;
  background: #f8fafc;
  border-radius: 8px;
}

.detail-icon {
  font-size: 18px;
}

.detail-text {
  font-size: 13px;
  color: #475569;
  font-weight: 500;
}

.booking-card-footer {
  padding: 16px;
  background: #fafafa;
  border-top: 1px solid #e5e7eb;
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

/* 空状态 */
.empty-state {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  gap: 16px;
}

.empty-icon {
  font-size: 64px;
  opacity: 0.5;
}

.empty-text {
  font-size: 16px;
  color: #94a3b8;
}

/* 切换按钮 */
.toggle-button {
  position: absolute;
  z-index: 999999999;
  cursor: pointer;
  top: 30%;
  padding: 8px;
  border-radius: 8px;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.toggle-button:hover {
  transform: scale(1.1);
}

.imgright {
  right: 0;
  background-color: #f5f5f5;
}

.imgright1 {
  left: 0;
  background-color: #fff;
}

/* 聊天区域 */
.chat-section {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.chat-container {
  height: 100%;
  background: rgba(255, 255, 255, 0.98);
  border-radius: 20px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-header {
  background: linear-gradient(135deg, #2193b0 0%, #6dd5ed 100%);
  padding: 20px 24px;
  color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-radius: 20px 20px 0 0;
}

.chat-title {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
}

.chat-status {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(255, 255, 255, 0.2);
  padding: 6px 12px;
  border-radius: 20px;
}

.status-dot {
  width: 8px;
  height: 8px;
  background: #10b981;
  border-radius: 50%;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

.status-text {
  font-size: 14px;
  font-weight: 500;
}

/* 消息容器 */
.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  background: linear-gradient(to bottom, #f8fafc, #e2e8f0);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.messages-container::-webkit-scrollbar {
  width: 6px;
}

.messages-container::-webkit-scrollbar-track {
  background: transparent;
}

.messages-container::-webkit-scrollbar-thumb {
  background: rgba(33, 147, 176, 0.3);
  border-radius: 3px;
}

.messages-container::-webkit-scrollbar-thumb:hover {
  background: rgba(33, 147, 176, 0.5);
}

/* 消息气泡 */
.message-item {
  display: flex;
  animation: slideIn 0.3s ease-out;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.user-message {
  justify-content: flex-end;
}

.user-message .message-bubble {
  background: linear-gradient(135deg, #2193b0 0%, #6dd5ed 100%);
  color: white;
  border-radius: 18px 18px 4px 18px;
}

.bot-message {
  justify-content: flex-start;
}

.bot-message .message-bubble {
  background: white;
  color: #2d3748;
  border-radius: 18px 18px 18px 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.message-bubble {
  max-width: 70%;
  padding: 12px 16px;
  word-wrap: break-word;
}

.message-content {
  font-size: 15px;
  line-height: 1.6;
  white-space: pre-wrap;
}

.message-time {
  font-size: 11px;
  margin-top: 6px;
  opacity: 0.7;
}

.user-message .message-time {
  text-align: right;
}

/* 输入区域 */
.input-container {
  padding: 20px 24px;
  background: white;
  border-top: 1px solid #e5e7eb;
}

.input-wrapper {
  display: flex;
  gap: 12px;
  align-items: flex-end;
}

.chat-input {
  flex: 1;
}

.chat-input ::v-deep .el-textarea__inner {
  border-radius: 12px;
  border: 2px solid #e5e7eb;
  padding: 12px 16px;
  font-size: 15px;
  transition: all 0.3s ease;
  resize: none;
}

.chat-input ::v-deep .el-textarea__inner:focus {
  border-color: #2193b0;
  box-shadow: 0 0 0 3px rgba(33, 147, 176, 0.1);
}

.send-button {
  width: 48px;
  height: 48px;
  background: linear-gradient(135deg, #2193b0 0%, #6dd5ed 100%);
  border: none;
  transition: all 0.3s ease;
}

.send-button:hover:not(:disabled) {
  transform: scale(1.1);
  box-shadow: 0 4px 12px rgba(33, 147, 176, 0.4);
}

.send-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.send-icon {
  font-size: 20px;
}

.span1 {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.drawerClass ::v-deep .el-drawer__container .el-drawer .el-drawer__header {
  display: none;
}
</style>
